package com.tensquare.article.dao;

import com.tensquare.article.pojo.Comment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.tensquare.article.pojo.Article;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 文章数据访问接口
 * @author Administrator
 *
 */
public interface ArticleDao extends JpaRepository<Article,String>,JpaSpecificationExecutor<Article>{
    /**
     * 审核
     * @param id
     */
    @Modifying  //在@Query注解中编写JPQL实现DELETE和UPDATE操作的时候必须加上@modifying注解，
    // 以通知Spring Data 这是一个DELETE或UPDATE操作。JPQL没有INSERT操作
    @Query("update Article set state='1' where id=?1")
    public void examine(String id);

    /**
     * 点赞
     * @param id
     * @return
     */
    @Modifying
    @Query("update Article a set thumbup=thumbup+1 where id=?1")
    public int updateThumbup(String id);

    //根据文章id设置标签列表
    @Modifying
    @Query(value = "insert into tb_la(aid,lid) values(?,?)",nativeQuery = true)
    public void setArticleLabel(String aid,String labelid);

    @Query(value = "select title from tb_article where userid=? and id!=? limit 4",nativeQuery = true)
    List<String> getOtherArticles(String uid, String aid);

    @Query(value = "select title from tb_article order by visits desc limit 4",nativeQuery = true)
    List<String> getHotArticles();

    //获取我的文章列表，分页
    @Query(value = "select * from tb_article where userid=? order by createtime desc",nativeQuery = true)
    Page<Article> getMyArticles(String userId, Pageable pageable);
}
